package com.wom.blocks;

import java.awt.Color;
import java.awt.Graphics;

import com.wom.blockfeatures.BlockRenderer;
import com.wom.blockfeatures.BlockUtils;
import com.wom.blockfeatures.BreakableBlock;
import com.wom.blockfeatures.FallingBlock;
import com.wom.gui.BlockTypes;
import com.wom.worldmaker.GameProperties;

/**
 * SandBlock which uses the FallingBlock properties
 * Cascade of falling blocks occurs when the block below is empty
 * @author David
 *
 */
public class SandBlock extends AbstractBlock {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public FallingBlock canFall;
	public BlockRenderer renderble;
	public BreakableBlock breakable;
	public boolean falling=false;
	public int fallTime=0;
	public int maxFallTime=14;

	public SandBlock(int xPos, int yPos) {
		super(xPos, yPos);
		int[] tileId={1,0};
		setId(tileId);

		setName("Sand");
		breakable=new BreakableBlock(200, 3);
		canFall=new FallingBlock();
		renderble=new BlockRenderer();
		setColor(Color.YELLOW);

	}

	@Override
	public void renderBlock(Graphics g) {
		renderble.renderBlock(this, g);
		//renderble.renderBlockFrame(this, g, Color.WHITE);
	}

	@Override
	public void updateBlock() {
		setBounds(getxPos()* GameProperties.blockSize, getyPos()*GameProperties.blockSize,GameProperties.blockSize, GameProperties.blockSize);


		// sand can fall if an air block is beneath it
		if(!breakable.isDead){
			if(fallTime>=maxFallTime){

				fallTime=0;

				canFall.fall(this);
				falling=false;

				//GameWorld.player.mousePressedRight=false;
			}else{
				fallTime++;
				falling=true;
			}

		}






		// if a sand block is place or left with no other blocks in mid air it is remove and replaced with air
		if(!falling && BlockUtils.isIsolatedBlock(this, BlockTypes.air)){
			BlockUtils.changeInto(getxPos(),getyPos(),BlockTypes.air);
		}

		// if block is dead remove







	}



}
